﻿package mortal.game.manager.msgTip
{
    import com.gengine.core.frame.*;
    import com.gengine.global.*;
    import com.gengine.utils.pools.*;
    import mortal.game.manager.*;
    import mortal.game.model.*;
    import mortal.game.view.msgbroad.*;

    public class MsgRollRadioImpl extends Object
    {
        private var _msgs:Array;
        private var _timer:FrameTimer;
        private var _item:RollRadioItem;
        private var _hideTimer:int;
        private const _hideTotal:int = 60;
        private var _countTimer:int;
        private const _countTotal:int = 12;

        public function MsgRollRadioImpl()
        {
            this._msgs = [];
            return;
        }// end function

        private function startRollRadio() : void
        {
            if (!this._timer)
            {
                this._timer = new FrameTimer(1);
            }
            this._timer.addListener(TimerType.ENTERFRAME, this.frameScript);
            if (!this._timer.running)
            {
                this._timer.reset();
                this._timer.start();
            }
            return;
        }// end function

        private function stopRollRadio() : void
        {
            if (this._timer && this._timer.running)
            {
                this._timer.stop();
                this._timer.dispose();
            }
            return;
        }// end function

        private function hideRollRadio() : void
        {
            if (this._item)
            {
                this._item.dispose();
                if (this._item.parent)
                {
                    this._item.parent.removeChild(this._item);
                }
            }
            return;
        }// end function

        private function showRollRadio() : void
        {
            if (this._item)
            {
                LayerManager.msgTipLayer.addChild(this._item);
                this.stageResize();
            }
            return;
        }// end function

        private function frameScript(param1:FrameTimer) : void
        {
            if (this._msgs.length > 0)
            {
                this._hideTimer = 0;
                if (this._item)
                {
                    if (this._item.running)
                    {
                        this._item.frameScript();
                    }
                    else if (this._item.hasEnd && this._msgs[0] == this._item.info)
                    {
                        this._msgs.shift();
                        if (this._msgs.length > 0)
                        {
                            this._item.updateData(this._msgs[0]);
                            this.showRollRadio();
                        }
                    }
                    else if (this._countTimer >= this._countTotal)
                    {
                        this._countTimer = 0;
                        this._item.updateData(this._msgs[0]);
                        this.showRollRadio();
                    }
                    else
                    {
                        var _loc_2:String = this;
                        var _loc_3:* = this._countTimer + 1;
                        _loc_2._countTimer = _loc_3;
                    }
                }
                else
                {
                    this._item = new RollRadioItem();
                    this._item.updateData(this._msgs[0]);
                    this.showRollRadio();
                }
            }
            else if (this._hideTimer >= this._hideTotal)
            {
                this.hide();
            }
            else
            {
                var _loc_2:String = this;
                var _loc_3:* = this._hideTimer + 1;
                _loc_2._hideTimer = _loc_3;
            }
            return;
        }// end function

        public function showMsg(param1:String, param2:int, param3:int, param4:Boolean = false) : void
        {
            var _loc_6:int = 0;
            var _loc_7:RollRadioInfo = null;
            var _loc_5:* = ObjectPool.getObject(RollRadioInfo);
            _loc_5.str = param1;
            _loc_5.totalCount = param2;
            _loc_5.speed = param3;
            _loc_5.isFirst = param4;
            if (param4 && this._msgs.length > 1)
            {
                _loc_6 = 1;
                while (_loc_6 < this._msgs.length)
                {
                    
                    _loc_7 = this._msgs[_loc_6] as RollRadioInfo;
                    if (!_loc_7.isFirst)
                    {
                        this._msgs.splice(_loc_6, 0, _loc_5);
                        break;
                    }
                    if (_loc_6 == (this._msgs.length - 1))
                    {
                        this._msgs.push(_loc_5);
                        break;
                    }
                    _loc_6++;
                }
            }
            else
            {
                this._msgs.push(_loc_5);
            }
            this.startRollRadio();
            return;
        }// end function

        public function stageResize() : void
        {
            if (this._item && this._item.parent)
            {
                this._item.x = (Global.stage.stageWidth - this._item.width) / 2;
                this._item.y = 55;
            }
            return;
        }// end function

        public function hide() : void
        {
            this.stopRollRadio();
            this.hideRollRadio();
            return;
        }// end function

    }
}
